import { anyGet, anyPost } from '@/network';
import type { ProviderChannelConfig } from './ProviderChannelConfig';
import {
  getMockProviderChannelConfigData,
  mockAddProviderChannelConfig,
  mockEditProviderChannelConfig,
  mockDeleteProviderChannelConfig,
  mockFetchProviderChannelConfigDetail
} from './mockData';

// 是否使用Mock数据
const USE_MOCK_DATA = false;

/**
 * 查询条件
 */
export interface QueryParams {
  page?: number;
  size?: number;
  providerId?: number;
  channelId?: number;
  status?: number;
}

/**
 * 分页查询服务商渠道配置列表
 */
export const apiProviderChannelConfigFetchList = (params: QueryParams) => {
  if (USE_MOCK_DATA) {
    return getMockProviderChannelConfigData(params.page || 1, params.size || 10);
  }
  return anyGet('/api/v1/miaoma/manual/provider-channel-config/list', params);
};

/**
 * 根据ID获取服务商渠道配置详情
 */
export const apiProviderChannelConfigFetchDetail = (id: number) => {
  if (USE_MOCK_DATA) {
    return mockFetchProviderChannelConfigDetail(id);
  }
  return anyGet(`/api/v1/miaoma/manual/provider-channel-config/${id}`);
};

/**
 * 添加服务商渠道配置
 */
export const apiProviderChannelConfigAdd = (data: ProviderChannelConfig) => {
  if (USE_MOCK_DATA) {
    return mockAddProviderChannelConfig(data);
  }
  return anyPost('/api/v1/miaoma/manual/provider-channel-config/save', data);
};

/**
 * 更新服务商渠道配置
 */
export const apiProviderChannelConfigEdit = (data: ProviderChannelConfig) => {
  if (USE_MOCK_DATA) {
    return mockEditProviderChannelConfig(data.id!, data);
  }
  return anyPost('/api/v1/miaoma/manual/provider-channel-config/update', data);
};

/**
 * 删除服务商渠道配置
 */
export const apiProviderChannelConfigDelete = (id: number) => {
  if (USE_MOCK_DATA) {
    return mockDeleteProviderChannelConfig(id);
  }
  return anyPost(`/api/v1/miaoma/manual/provider-channel-config/delete/${id}`);
};

/**
 * 批量删除服务商渠道配置
 */
export const apiProviderChannelConfigBatchDelete = (ids: number[]) => {
  if (USE_MOCK_DATA) {
    return Promise.all(ids.map(id => mockDeleteProviderChannelConfig(id)))
      .then(() => ({ success: true, message: '批量删除成功' }));
  }
  return anyPost('/api/v1/miaoma/manual/provider-channel-config/batchDelete', ids);
};

/**
 * 更新配置状态
 */
export const apiProviderChannelConfigUpdateStatus = (id: number, status: number) => {
  if (USE_MOCK_DATA) {
    return mockEditProviderChannelConfig(id, { status });
  }
  return anyPost(`/api/v1/miaoma/manual/provider-channel-config/updateStatus?id=${id}&status=${status}`);
};